package Narzedzia;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class PlikTekstowy {
 
	private final static char commentTag = '#';

	public static String wczytajZawartoscPliku(String sciezka) throws IOException {
		File file = new File(sciezka);
		StringBuilder result = new StringBuilder();
		String str;
		BufferedReader in = new BufferedReader(new FileReader(file));
		while ((str = in.readLine()) != null) {
			if (str.indexOf(commentTag, 0) != 0) {
				result.append(str + "\r\n");
			}

		}
		in.close();
		return result.toString();
	}
	
	public static void zapiszZawartoscDoPliku(String filePath, String data) throws IOException {
		File file = new File(filePath);
		BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
		out.write(data, 0, data.length());
		out.close();

	}

	public static String[] czesci(String zawartosc, String regex) {

		String[] tmp = zawartosc.split(regex);
		String[] sub = new String[tmp.length];
		for (int i = 0; i < tmp.length; i++) {
			if (!tmp[i].equalsIgnoreCase("")) {
				sub[i] = (tmp[i].trim());
			}
		}
		return sub;
	}
	
	public static String getSubToRegex(String content, String regex) {

		return content.substring(0, content.indexOf(regex));
	}

	public static String getSubBetweenChars(String content, char first, char last) {
		return content.substring(content.indexOf(first) + 1, content.indexOf(last));
	}

	public static String getSubAfterRegex(String content, String regex) {
		return content.substring(content.indexOf(regex) + regex.length(), content.length());
	}
}
